package com.qymerge.manage.base.controller;

import com.google.common.reflect.TypeToken;
import com.qymerge.manage.base.ro.MetaBudgetProductRO;
import com.qymerge.manage.base.service.IMetaBudgetProductService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Title: IMetaProfitReportItemManageController</p>
 * <p>Description: 利润表报表项目配置</p>
 *
 * @author Xiao
 * @date 2025/4/29 17:18
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "base/budgetProduct")
public class IMetaBudgetProductManageController extends BaseController {

    @Autowired
    private IMetaBudgetProductService metaBudgetProductService;
    
    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaBudgetProductRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaBudgetProductRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaBudgetProductRO ro = paramValue.getData();

        DataTableRO data = metaBudgetProductService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<MetaBudgetProductRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaBudgetProductRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaBudgetProductRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getId(), "ID必须");
        MetaBudgetProductRO resultRO = metaBudgetProductService.detail(ro.getId());

        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<MetaBudgetProductRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaBudgetProductRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaBudgetProductRO ro = paramValue.getData();

        ActionResultRO resultRO = metaBudgetProductService.save(ro);
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<CommonIdsDTO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<CommonIdsDTO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        CommonIdsDTO commonIdsDTO = paramValue.getData();

        ActionResultRO obj = metaBudgetProductService.delete(commonIdsDTO.getIds());
        PrintCodeUtils.print(response, obj, callback);
    }

    /**
     * 导入execl格式数据
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("/importData")
    public void importData(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                           @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<CommonIdsDTO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<CommonIdsDTO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        CommonIdsDTO ro = paramValue.getData();

        if (!(request instanceof MultipartHttpServletRequest)) {
            PrintCodeUtils.printError(response, "提交方式必须是 multipart/form-data", callback);
        }
        MultipartFile uploadify = ((DefaultMultipartHttpServletRequest) request).getFile("file");
        if (uploadify == null || uploadify.isEmpty()) {
            PrintCodeUtils.printError(response, "请选择文件", callback);
        }

        ActionResultRO resultDTO = metaBudgetProductService.importData2(uploadify, ro.getBudgetId());
        PrintCodeUtils.print(response, resultDTO, callback);
    }


}
